package com.shaoshaohuo.app.utils;

import android.app.Activity;
import android.content.Context;

import com.alipay.sdk.PayResult;
import com.alipay.sdk.app.PayTask;
import com.shaoshaohuo.app.wxapi.Constants;
import com.shaoshaohuo.app.wxapi.WXPayEntryActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * 支付工具类
 * Created by Administrator on 2017/6/14.
 */

public class PayUtils {

    /**
     * 支付宝 ，需要异步请求
     */
    public static void payzfb(Activity activity, String sign, PayListener payListener) {

//         构造PayTask 对象
        PayTask alipay = new PayTask(activity);
        // 调用支付接口
        String result = alipay.pay(sign, true);

        PayResult resultObj = new PayResult(result);
        if (resultObj.getResultStatus().equals("9000")) {
            //支付宝9000是成功
            payListener.Success();
        } else {
            payListener.Erro(result);
        }

    }

    /**
     * 微信支付
     */
    public static void wxpay(Context context, String sign,
                             String appid, String partnerid,
                             String prepayId, String noncestr, String timeStamp,String ordier) {

        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, Constants.APP_ID);
        PayReq req = new PayReq();
        msgApi.registerApp(Constants.APP_ID);

        if (!msgApi.isWXAppInstalled()) {
            ToastUtil.toast("没有安装微信");
            return;
        }
        if (!msgApi.isWXAppSupportAPI()) {
            ToastUtil.toast("当前版本不支持支付功能");
            return;
        }

        WXPayEntryActivity.setoride(ordier);
        req.sign = sign;
        req.appId = appid;
        req.partnerId = partnerid;
        req.prepayId = prepayId;
        req.packageValue = "Sign=WXPay";
        req.nonceStr = noncestr;
        req.timeStamp = timeStamp;
        req.extData = "app data";
        msgApi.sendReq(req);

    }

    public interface PayListener {
        void Success();

        void Erro(String result);
    }


}
